package br.com.sgc.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.sgc.entities.financeiro.ContasPagar;
import br.com.sgc.entities.financeiro.PedidoCompra;
import br.com.sgc.entities.geral.Clinica;
import br.com.sgc.enuns.TipoOrdenacao;

@Stateless
public class ContasPagarDao extends GenericDao<ContasPagar> {

	private static final long serialVersionUID = -7719537235665259902L;

	@SuppressWarnings("unchecked")
	public List<ContasPagar> findAllByPedidoCompra(PedidoCompra pedidoCompra) {
		Criteria criteria = getCriteria(ContasPagar.class);
		criteria.add(Restrictions.eq("pedidoCompra.id", pedidoCompra.getId()));
		List<ContasPagar> list = criteria.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<ContasPagar> findAll(ContasPagar modelClass, Long clinicaId,
			Integer startPage, Integer maxPage, String orderField,
			Boolean initializeDependecies, TipoOrdenacao ordenacao) {
		
		Criteria criteria = getCriteria((Class<ContasPagar>)modelClass.getClass());
		criteria.setFirstResult(startPage);
		criteria.setMaxResults(maxPage);	
		createCriteriaRestrictions(criteria, modelClass);
		criteria.addOrder(Order.asc("dataPrevisaoPagamento"));
		criteria.addOrder(Order.asc("numeroParcela"));
		
		List<ContasPagar> lista = criteria.list();
		for(ContasPagar c: lista){
			c.getPedidoCompra().toString();
		}
			
		return lista;
	}
	
	private void createCriteriaRestrictions(Criteria criteria, ContasPagar modelClass) {
		if(modelClass != null){
			if(modelClass.getClinica() != null && modelClass.getClinica().getId() != null){
				criteria.add(Restrictions.eq("clinica.id", modelClass.getClinica().getId()));
			}
			if(modelClass.getPedidoCompra() != null && modelClass.getPedidoCompra().getNumeroPedido() != null && !modelClass.getPedidoCompra().getNumeroPedido().isEmpty()){
				criteria.createAlias("pedidoCompra", "pedidoCompra");
				criteria.add(Restrictions.eq("pedidoCompra.numeroPedido", modelClass.getPedidoCompra().getNumeroPedido().replaceAll("-PED", "").concat("-PED")));
			}
			if(modelClass.getQuitado() != null){
				criteria.add(Restrictions.eq("quitado", modelClass.getQuitado()));
			}
			
			
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Long count(ContasPagar modelClass, Long clinicaId) {
		Long size = 0L;
		Criteria criteria = getCriteria((Class<ContasPagar>)modelClass.getClass());
		criteria.setProjection(Projections.rowCount());
		createCriteriaRestrictions(criteria, modelClass);
		size = (Long) criteria.uniqueResult();
        return size;
	}

	public BigDecimal findTotalContasPorData(Date dataCaixa, Clinica clinica) {
		Criteria criteria = getCriteria(ContasPagar.class);
		criteria.add(Restrictions.eq("clinica.id", clinica.getId()));
		criteria.add(Restrictions.eq("quitado", Boolean.TRUE));
		criteria.add(Restrictions.eq("dataRealPagamento", dataCaixa));
		criteria.setProjection(Projections.sum("valorPagamento"));
		return (BigDecimal)criteria.uniqueResult();
	}

}
